import { defineStore } from 'pinia';

interface globalType {
  id_temp: number | null;
  editType: string;
  data: {
    backgroundColor: string;
    bgImgUsed: boolean;
    bgImg: string;
    dftRender: string;
    xAxisMax: number;
    yAxisMax: number;
    devicetype?: number | null; //公共弹窗才有这个属性
  }
}
export const useGlobalStore = defineStore('ztGlobal', {
  state: (): globalType => ({
    id_temp: null,
    editType: 'globalNormal',
    data: {
      backgroundColor: 'rgba(0, 61, 81, 1)',
      bgImgUsed: false,
      bgImg: '',
      dftRender: '自适应',
      xAxisMax: 1920,
      yAxisMax: 1080,
      devicetype: null,
    },
  }),
  getters: {
    getGlobal(state) {
      return (): globalType => {
        return {
          id_temp: state.id_temp,
          editType: state.editType,
          data: state.data,
        }
      }
    }
  },
  actions: {
    setID(id: number | null): void {
      this.id_temp = id;
    },
    setData(msg: Partial<globalType>): void {
      for (const key in msg) {
        if (Object.prototype.hasOwnProperty.call(msg, key)) {
          (this.data as any)[key] = (msg as any)[key]!;
        }
      }
    }
  },
});